::p_load(tmap, sf, tidyverse) pacman
In-class Ex 01
Load R packages
The following code chunk loads the necessary packages:
- tmap: for thematic mapping
- sf: for geospatial data handling
- tidyverse: for aspatial data transformation
Preparing the OD Data
Import the Passenger Volume by Origin Destination dataset downloaded from LTA DataMall using read_csv()
of the readr package:
<- read_csv("data/aspatial/origin_destination_bus_202308.csv")
odbus08
# bus09 <- read_csv("data/aspatial/origin_destination_bus_202309.csv")
# bus10 <- read_csv("data/aspatial/origin_destination_bus_202310.csv")
odbus08 is a tibble dataframe. However, ORIGIN_PT_CODE
and DESTINATION_PT_CODE
are in character format. These are transformed into factors (categorical data type) for further analysis.
<- odbus08 %>%
odbus08 mutate(
ORIGIN_PT_CODE = as.factor(ORIGIN_PT_CODE),
DESTINATION_PT_CODE = as.factor(DESTINATION_PT_CODE)
)
Extracting the data for Analysis
Create a new dataframe origtrip_7_9 by extracting Origin busstop codes and number of trips for weekdays between 7 and 9 o’clock:
<- odbus08 %>%
origtrip_7_9 filter(
== "WEEKDAY"
DAY_TYPE %>%
) filter(
>= 7 & TIME_PER_HOUR <= 9
TIME_PER_HOUR %>%
) group_by(
ORIGIN_PT_CODE%>%
) summarise(
TRIPS = sum(TOTAL_TRIPS)
%>%
) ungroup()
Loading the geospatial data
<- st_read(
busstop dsn = "data/geospatial",
layer = "BusStop"
%>%
) # Assigning the right EPSG code based on coordinate system
st_transform(
crs = 3414
)
Reading layer `BusStop' from data source
`C:\haileycsy\ISSS624-AGA\In-class_Ex\ice1\data\geospatial'
using driver `ESRI Shapefile'
Simple feature collection with 5161 features and 3 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 3970.122 ymin: 26482.1 xmax: 48284.56 ymax: 52983.82
Projected CRS: SVY21
<- st_read(
mpsz dsn = "data/geospatial",
layer = "MPSZ-2019"
%>%
) # Assigning the right EPSG code
st_transform(
crs = 3414
)
Reading layer `MPSZ-2019' from data source
`C:\haileycsy\ISSS624-AGA\In-class_Ex\ice1\data\geospatial'
using driver `ESRI Shapefile'
Simple feature collection with 332 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 103.6057 ymin: 1.158699 xmax: 104.0885 ymax: 1.470775
Geodetic CRS: WGS 84